package br.com.friendzoone.util;


import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;

public class PropertyUtil {

	private static PropertyUtil instance = null;
	
	private String arquivoConfiguracao;

	/**
	 * @return Retorna instancia da classe.
	 */
	public static PropertyUtil getInstance() {
		if (instance == null) {
			instance = new PropertyUtil();
		}
		return instance;
	}

	public PropertyUtil( final String arquivoConfiguracao){
		this.arquivoConfiguracao = arquivoConfiguracao;
		mapProperties = new HashMap<String, String>();
		obterMensagem();
	}
	private HashMap<String, String> mapProperties;

	/**
	 * Construtor da classe.
	 */
	public PropertyUtil() {
		mapProperties = new HashMap<String, String>();
		obterMensagem();
	}

	/**
	 * @param key
	 *            Palavra chave.
	 * @return Retorna mensagem.
	 */
	public String getProperty(String key) {
		return mapProperties.get(key);
	}
	public int getPropertyInt(final String key){
		return Integer.parseInt( mapProperties.get( key ) );
	}

	private void obterMensagem() {
		Properties prop = new Properties();
		try {
			prop.load(getClass().getResourceAsStream(arquivoConfiguracao));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		Iterator<Object> itKeys = prop.keySet().iterator();

		while (itKeys.hasNext()) {
			String key = (String) itKeys.next();
			String value = prop.getProperty(key);
			mapProperties.put(key, value);
		}
	}

}

